﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FreeProperty.BusinessLogic;
using FreeProperty.Contract;
using System.Text;
using FreeProperty.Views.Shared;
using System.IO;

namespace FreeProperty.Views.ProviderDirectory
{
    public partial class EmailResults : SiteBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (null == Session[FreePropertyConstants.SESSION_SEARCH_OBJECT])
            {
                Response.Redirect("~/Views/ProviderDirectory/SearchPassportProvider.aspx");
            }
            if (hdnMode.Value == "Email")
            {
                try
                {
                    string sAttachedHtml =  GetHtmlString();

                    //FreePropertyUtil.SendMail(tbEmailAddress.Value.Trim(), GetControlHTML(oPrintResults));
                    FreePropertyUtil.SendMail(tbEmailAddress.Value.Trim(), sAttachedHtml);
                    Response.Redirect("~/Views/ProviderDirectory/SearchPassportProvider.aspx");
                }
                catch
                {
                    lblError.Text = "Internal server error in sending Email.";
                }
            }
        }

        private string GetHtmlString()
        {
            List<ProviderVO> lstSearch = FreePropertyUtil.GetSearchResults();

            StringBuilder sbTop = new StringBuilder();
            sbTop.Append("<table cellspacing='0' cellpadding='4' border='0' style='width:100%;border-collapse: collapse; font-family: Verdana,Arial,Helvetica,sans-serif; font-size: 11px;' id='grdSearchResults'><tbody>");
            string bottom = "</tbody></table>";
            StringBuilder sb = new StringBuilder();
            //Headerstyle="background-color:#afcdd1"
            sb.Append("<tr style='background-color:#afcdd1'>");
            sb.Append("<th scope='col'>Provider</th><th scope='col'>Services</th><th scope='col'>Provider Details</th><th scope='col'>Map</th>");
            sb.Append("</tr>");
            //Items
            int i=0;
            foreach (ProviderVO item in lstSearch)
            {
                // check made to save from excessive html rendering
                if (i == FreePropertySettings.MaxEmailItemsCount)
                    break;
                if (i % 2 == 0)
                {
                    sb.Append("<tr >");
                }
                else
                {
                    sb.Append("<tr style='background-color:#e4ebec'>");
                }
                string providerString = item.FirstName + " " + item.LastName + "<br>" + item.LocationName + "<br>" + item.Address + ",<br>" + item.City + ", " + item.State + " " + item.ZipCode + "<br>" + item.Phone;

                string servicesString = "<img style='border-width: 0px;' src='" + GetBaseURL() + "Styles/Images/eyechart.gif' />";
                string providerdetString = "Eyewear retailer:" + item.Class + "<br> Language:" + item.Language + "<br> Accepting Patients:" + item.AcceptingNewPatients + "<br> Board Certified:" + item.BoardCertified + "<br> Gender:" + item.Gender;

                StringBuilder sbAddress = new StringBuilder();
                sbAddress.Append(item.Address).Append(" ").Append(item.City).Append(" ").Append(item.State).Append(" ").Append(item.ZipCode);
                var url = "http://maps.google.co.in/maps?f=q&source=s_q&hl=en&geocode=&q=" + HttpContext.Current.Server.UrlEncode(sbAddress.ToString());
                string mapString = "<a target='_blank' href='" + url + "'><img style='border-width: 0px;' src='" + GetBaseURL() + "Styles/Images/mapq.gif' /></a>";

                sb.Append("<td style='border: 1px solid #CCCCCC;'>" + providerString + "</td>");
                sb.Append("<td style='border: 1px solid #CCCCCC;' align='center'>" + servicesString + "</td>");
                sb.Append("<td style='border: 1px solid #CCCCCC;'>" + providerdetString + "</td>");
                sb.Append("<td style='border: 1px solid #CCCCCC;' align='center'>" + mapString + "</td>");
                sb.Append("</tr>");
                i++;
            }

            string sTable = sbTop.ToString() + sb.ToString() + bottom;
            return sTable;
        }
        
        



    }
}